package com.umeox.iqibla.custom_tasbih.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Build
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Adapter
import com.umeox.iqibla.custom_tasbih.R
import com.umeox.moto.watch.themes.app.ThemeManager
import com.umeox.moto.watch.themes.widget.TextView
import com.umeox.watch.moto.apiclient.model.TaskRecordDTO

class CustomTasbihTaskListAdapter : Adapter<CustomTasbihTaskListAdapter.Holder>() {

    private val dataList = mutableListOf<TaskRecordDTO>()

    private var onItemClickListener: OnItemClickListener? = null

    @SuppressLint("NotifyDataSetChanged")
    fun setDataList(list: List<TaskRecordDTO>) {
        dataList.clear()
        dataList.addAll(list)
        notifyDataSetChanged()
    }

    fun setOnItemClickListener(listener: OnItemClickListener?) {
        onItemClickListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
        val view: View = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_custom_tasbih_task, parent, false)
        return Holder(view)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: Holder, position: Int) {
        holder.bind(position, dataList[position], onItemClickListener)
    }

    class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val tvNumber: android.widget.TextView = itemView.findViewById(R.id.tv_number)
        private val tvClock: TextView = itemView.findViewById(R.id.tv_clock)
        private val tvContent: TextView = itemView.findViewById(R.id.tv_content)
        private val sbProgress: SeekBar = itemView.findViewById(R.id.sb_progress)
        private val tvProgress: TextView = itemView.findViewById(R.id.tv_progress)

        @SuppressLint("SetTextI18n", "UseCompatLoadingForDrawables")
        fun bind(position: Int, taskRecordDTO: TaskRecordDTO, listener: OnItemClickListener?) {
            tvNumber.text = "${position + 1}"
            try {
                tvClock.text = taskRecordDTO.reminderTime
                tvContent.text = taskRecordDTO.title
                sbProgress.max = taskRecordDTO.additionalInfo.goals
                sbProgress.progress = taskRecordDTO.additionalInfo.completed
                sbProgress.isEnabled = false
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    if (ThemeManager.getInstance().currentTheme == 0) {
                        //暗色
                        sbProgress.progressDrawable =
                            itemView.context.getDrawable(R.drawable.custom_progress_bar)
                    } else {
                        //亮色
                        sbProgress.progressDrawable =
                            itemView.context.getDrawable(R.drawable.custom_progress_bar_light)
                    }
                }
                tvProgress.text =
                    "${taskRecordDTO.additionalInfo.completed}/${taskRecordDTO.additionalInfo.goals}"
                itemView.setOnClickListener {
                    listener?.onItemClick(position, taskRecordDTO)

                }
            } catch (e: Exception) {
                tvClock.text = "-"
                tvContent.text = "-"
                sbProgress.max = 100
                sbProgress.progress = 100
                tvProgress.text = "-/-"
                e.printStackTrace()
                itemView.setOnClickListener { }
            }
        }
    }

    interface OnItemClickListener {
        fun onItemClick(position: Int, data: TaskRecordDTO)
    }
}